<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="/static/css/stylemain.css">
<link rel="stylesheet" href="/static/layui/css/layui.css">

<link rel="stylesheet" href="/static/layui/css/treeTable.css">
<link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
<script src="/static/layui/layui.all.js"></script>
<script type="text/javascript" src="/static/js/jquery.js"></script>
<style>
   .layui-form-checkbox {
       margin-top:5px !important;
   }
</style>
</head>
<body>
<table class="layui-hide" id="tbl_missionlist" lay-filter="tbl_missionlist"></table>
<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">   
	<button id="btn_stop" class="layui-btn layui-btn-sm layui-btn-danger" lay-event="stopmission"><i class="layui-icon layui-icon-404"></i> 终止任务</button>
	<input style="width:150px;height:24px;font-size: 12px;" id="ownersearch" class="ckinput" type="text" placeholder="使用人查询"></input> 
	<input style="width:150px;height:24px;font-size: 12px;margin-left:5px" id="locksearch" class="ckinput" type="text" placeholder="识别码查询"></input>
	
	<button type="button" id="btn_search" style="margin-left:5px" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="search"><i class="fa fa-search" aria-hidden="true"></i>查询</button>
  </div>
  </script>

<script type="text/javascript">
var table = layui.table;
var form = layui.form;
var _slock = "";
var _sowner = "";
var _missionid = "";
layui.use([], function(){
	table.on('toolbar(tbl_missionlist)', function(obj){
		//debugger;
		var checkStatus = table.checkStatus(obj.config.id);
		switch(obj.event){	      
	      case 'stopmission':
	    	  if(checkStatus.data.length == 0){
	      		layer.msg('请先选择任务!');
	      		return;
	      	  }
	    	  curopt = 'stopmission';
	    	  u_stop('','');
	          break;  
	      case 'search':
	    	  _sowner = $('#ownersearch').val();
	  		  _slock = $('#locksearch').val();
	  		  debugger;
	  		  getMissionList();
	          break; 
	    };
		
		
	    
	});
	
});


function child(missionid,active){
	debugger;
	_missionid = missionid;
	
	getMissionList();
	if(active == 1){
		$("#btn_stop").css("display","none");
	}
}


function getMissionList(){
	var datastring = '{"missionid":"' + _missionid + '","owner":"' + _sowner +'","locksn":"' + _slock + '"}';
	$.ajax({
		type:"post",
		contentType: "application/json;charset=UTF-8",
		url: "getMissionList_template",
		data:datastring,
		async:false,
     	dataType:"json",
        success: function(msg) {    //数据提交成功时返回数据
        	debugger;
        	if(msg.status == "1"){
				tablerender(msg);
			}else{
				layer.msg(msg.desc);
			}	
        	
        },        
        error : function(e){
            alert(e.status);
            alert(e.responseText);
        }

	});
}

function tablerender(data){	
	table.render({
		  elem: '#tbl_missionlist'
		  ,toolbar: '#toolbarDemo'
		  ,defaultToolbar:[]
		  ,cellMinWidth: 50
		  ,cols: [
			  [
			  {type:'checkbox', fixed: 'left'}
			  ,{field:'locksn', title: '锁具识别码', width:160} 
			  ,{field:'oper_reals', title: '授权人'} 
			  ,{field:'stime', title: '生效时间', width:160} 
			  ,{field:'etime', title: '失效时间', width:160}
			  ,{field:'active', title: '状态', width:80, align: 'center', templet: '#statusTpl'}
			  ,{field:'stopreason', title: '终止原因'}
		  ]
			  ]
		  ,data:data.datalist
		  ,limit: Number.MAX_VALUE
		  ,height: 'full-30',
		  done: function(res, curr, count){
		    $('#locksearch').val(_slock);
		    $('#ownersearch').val(_sowner);
		    var data = res.data;
            var allck = true;
            for (var item in data) {
                if (data[item].active == 0) { 
                    allck = false;
                }
                break;
            }
            if (!allck) {
                $(".layui-table-header").find("input[name = 'layTableCheckbox'][lay-filter='layTableAllChoose']").each(function() {
                    $(this).attr("disabled", 'disabled').next().removeClass("layui-form-checked");
                    form.render('checkbox');
                });
            }
		    var i = 0;
            $(".layui-table-body.layui-table-main").find("input[name='layTableCheckbox']").each(function() {
                if (res.data[i].active == 0) { 
                    $(this).attr("disabled", 'disabled').removeAttr("checked");
                    form.render('checkbox');
                }
                i++;
            });
            i = 0;
            $(".layui-table-fixed.layui-table-fixed-l").find(".layui-table-body").find("input[name='layTableCheckbox']").each(function() {
                if (res.data[i].active == 0) { 
                    $(this).attr("disabled", 'disabled').removeAttr("checked");
                    form.render('checkbox');
                }
                i++;
            });
		  }
		});	

}

function getCheckedMissions(){
	//debugger;
	var checkStatus = table.checkStatus('tbl_missionlist');
	var data = checkStatus.data;
	return data;
}

function ischeckAll(){
	var checkStatus = table.checkStatus('tbl_missionlist');
	return checkStatus.isAll;
}

//监听表格选择
table.on('checkbox(tbl_missionlist)', function(obj){
	debugger;
    var mtr=obj.tr;
    var dx = obj.tr.children("td").children("div").children("div");
    var $dx =$(dx);
    if(obj.checked==true){
        $(mtr).css("color","green");
        $dx.addClass('layui-form-checked');

    }else{
        $(mtr).css("color","black");
        $dx.removeClass('layui-form-checked');
    }

});

function u_stop(ckname,ckpassword){
	var checkStatus = table.checkStatus('tbl_missionlist');
	layer.prompt({
		  formType: 2,
		  value: '',
		  title: '请输入终止原因',
		  area: ['300px', '30px'] //自定义文本域宽高
		}, function(value, index, elem){
			var ischeckAll = false;
			if(checkStatus.isAll && _slock != "" && _sowner != ""){
				ischeckAll = true;
			}
			var datastring = '{"missionid":"' + _missionid + '","stopreason":"' + value + '","isAll":' + ischeckAll +',"locklist":' + JSON.stringify(checkStatus.data) + ',"rname":"智能锁管理/任务管理/终止任务","ckname":"'+ckname+'","ckpassword":"'+ckpassword+'"}';
			$.ajax({
				type:"post",
				contentType: "application/json;charset=UTF-8",
				url: "stopmission_multi",
				data:datastring,
				async:false,
		     	dataType:"json",
		        success: function(msg) {    //数据提交成功时返回数据
		        	//debugger;
		        	if(msg.status == "1"){
		        		getMissionList();
					}else if(msg.status = "1211"){
						parent.$('#ckname').val('');
						parent.$('#ckpassword').val('');
						ckdialgo_auth();
					}else{
						layer.msg(msg.desc);
					}	
		        	
		        }

			});
			layer.close(index);
		}
	);
}

function ckdialgo_auth(){
	layer.open({
		  type: 1 //0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层） 
		  ,title: "复核" //标题
		  ,skin:"layui-layer-lan"   //layui-layer-lan:蓝色，layui-layer-molv：墨绿色，默认灰色
		  ,area: ['350px', '260px'] //宽度、高度
		  ,shade: 0.3
		  ,offset: 'auto'
		  ,anim:5 //弹出动画
		  ,shadeClose: false //弹框出现后，允许点击其它地方关闭
		  ,content: parent.$("#dlgck")     //内容
		  ,btn: ['确定', '取消'] //只是为了演示
		  ,success:function(layero){
			     //var mask = $(".layui-layer-shade");
			     //mask.appendTo(layero.parent());
			     parent.$('#ckname').focus();
			     //其中：layero是弹层的DOM对象
			}
		 ,yes: function(index, layero){
		    //按钮【按钮一】的回调
		    	userckopt();
		    	parent.$("#dlgck").css('display','none');
			 	layer.close(index);
		    	
			 	 
		  }
		  ,btn2: function(index, layero){
		    	//按钮【按钮二】的回调
		    
		    	parent.$("#dlgck").css('display','none'); 
		    	
		  }
		  ,cancel: function(){ 
			    //右上角关闭回调
			    parent.$("#dlgck").css('display','none'); 
			    
		 }
	});
}

function userckopt(){
	//debugger;
	var ckname = parent.$('#ckname').val();
	if(ckname == ''){
		layer.msg('请填写用户账号!');
		return;
	}
	var ckpassword = parent.$('#ckpassword').val();
	u_stop(ckname,$.base64.encode(ckpassword));	
}

</script>
<script type="text/html" id="statusTpl">
  {{#  if(d.active == '0'){ }}	
    <span style="color: #ef40b1;">终止</span>
  {{#  } else if(d.active == '1') { }}
    <span">授权</span>
  {{#  } }}
</script>
</body>
</html>
